package com.ev.k3cloudBusiness.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ev.framework.annotation.EvApiByToken;
import com.ev.framework.utils.R;
import com.ev.k3cloudBusiness.service.K3PurPoOrderService;
import com.ev.k3cloudBusiness.service.K3StkInStockService;
import com.ev.k3cloudBusiness.vo.K3PurOrderPushVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Api(value = "/", tags = "金蝶K3Cloud采购订单")
@RestController
public class K3PurPoOrderController {

    @Autowired
    K3PurPoOrderService k3PurPoOrderService;
    @Autowired
    K3StkInStockService k3StkInStockService;
    @EvApiByToken(value = "/apis/purPoOrder/pushPurInStockByIds", method = RequestMethod.POST, apiTitle = "根据明细内码下推采购入库单")
    @ApiOperation("根据明细内码下推采购入库单")
    public R getBySourceBillNo(@RequestBody @ApiParam("下推参数[{\"id\":1,\"stockId\":0,\"stockLocId\":0,\"qty\":12}]") K3PurOrderPushVo purPoOrderPushVo) {
        try {
            String result = k3PurPoOrderService.pushPurInStockByIds(purPoOrderPushVo);
            JSONObject resultObj = JSON.parseObject(result);
            if( resultObj.getJSONObject("Result").getJSONObject("ResponseStatus").getBooleanValue("IsSuccess") ) {
                //提交下推生成的入库单
                String ids=resultObj.getJSONObject("Result").getJSONObject("ResponseStatus").getJSONArray("SuccessEntitys").getJSONObject(0).getString("Id");
                String submitResult= k3StkInStockService.submitStkInStockByIds(ids);
                JSONObject submitResultObj = JSON.parseObject(submitResult);
                if(submitResultObj.getJSONObject("Result").getJSONObject("ResponseStatus").getBooleanValue("IsSuccess"))
                {
                    //审核下推生成的入库单
                    String auditResult= k3StkInStockService.auditStkInStockByIds(ids);
                    JSONObject auditResultObj = JSON.parseObject(auditResult);
                    if(auditResultObj.getJSONObject("Result").getJSONObject("ResponseStatus").getBooleanValue("IsSuccess"))
                    {
                        return R.ok();
                    }
                    else
                    {
                        //反审核下推生成入库单
                        String unauditResult= k3StkInStockService.unauditStkInStockByIds(ids);
                        JSONObject unauditResultObj = JSON.parseObject(unauditResult);
                        //删除下推生成的入库单
                        String deleteResult= k3StkInStockService.deleteStkInStockByIds(ids);
                        JSONObject deleteResultObj = JSON.parseObject(deleteResult);
                        return R.error(auditResultObj.getJSONObject("Result").getJSONObject("ResponseStatus").getJSONArray("Errors").toString());
                    }
                }
                else
                {
                    //删除下推生成的入库单
                    String deleteResult= k3StkInStockService.deleteStkInStockByIds(ids);
                    JSONObject deleteResultObj = JSON.parseObject(deleteResult);
                    return R.error(submitResultObj.getJSONObject("Result").getJSONObject("ResponseStatus").getJSONArray("Errors").toString());
                }

            } else {
                return R.error(resultObj.getJSONObject("Result").getJSONObject("ResponseStatus").getJSONArray("Errors").toString());
            }
        }
        catch (Exception ex) {
            return R.error(ex.getMessage());
        }
    }
}
